﻿using System;
using Hjt212Parser;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform.SG;
using log4net;


namespace Biz.Process
{
    /// <summary>
    /// 主要处理离线、在线
    /// </summary>
    public class ProcessorStatus
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorStatus));

        public static void Process0000(PlatformXpfContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.主实时监控.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID).ToList();
                    foreach (var item in items)
                    {
                        if (item.在线状态时间.HasValue && Math.Abs((datatime - item.在线状态时间.Value).TotalHours) > 1)
                        {
                            item.在线状态ID = 1202;
                            item.在线状态时间 = datatime;
                        }
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process0000ByMn(PlatformXpfContext pCtx, string mn)
        {
            try
            {
                if (string.IsNullOrEmpty(mn)) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.主实时监控.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID).ToList();
                    foreach (var item in items)
                    {
                        item.在线状态ID = 1202;
                        item.在线状态时间 = DateTime.Now;
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process9999(PlatformXpfContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.主实时监控.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID).ToList();
                    foreach (var item in items)
                    {
                        item.在线状态ID = 1201;
                        item.在线状态时间 = datatime;
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        public static void Process3081(PlatformXpfContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);

            var monitors = pCtx.主实时监控
                   .Where(t => t.企业基本信息ID == port.企业基本信息ID).Where(t => t.企业排口ID == port.ID);
            foreach (var monitor in monitors)
            {
                monitor.市电状态ID = 1102;
                monitor.市电状态时间 = datatime;
            }
            pCtx.SaveChanges();

        }
        public static void Process3082(PlatformXpfContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);

            var monitors = pCtx.主实时监控
                   .Where(t => t.企业基本信息ID == port.企业基本信息ID).Where(t => t.企业排口ID == port.ID);
            foreach (var monitor in monitors)
            {
                monitor.市电状态ID = 1101;
                monitor.市电状态时间 = datatime;
            }
            pCtx.SaveChanges();
        }

        public static void Process3083(PlatformXpfContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);

            var monitors = pCtx.主实时监控
                   .Where(t => t.企业基本信息ID == port.企业基本信息ID).Where(t => t.企业排口ID == port.ID);
            foreach (var monitor in monitors)
            {
                if (monitor.市电状态ID != 1101)
                {
                    monitor.市电状态ID = 1101;
                    monitor.市电状态时间 = datatime;
                }
            }
            pCtx.SaveChanges();
        }

        /// <summary>
        /// 1、处理阀门控制表
        /// 2、处理实时监控表
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process3071(PlatformXpfContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var port = CommonInformation.GetLetportByMn(mn);
                Log.Info("Port is null:" + (port == null));
                if (port == null) return;
                Log.Info("Port:" + port.MN号);
                var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                var per = Parser.GetStringByKey("Per", content);
                if (string.IsNullOrEmpty(per))
                {
                    per = Parser.GetStringByKey("Status", content);
                }
                Log.Info("Per:" + per);
                var qn = Parser.GetStringByKey("QN", content);
                var items = pCtx.阀门控制
                    .Where(t => t.企业基本信息ID == port.企业基本信息ID)
                    .Where(t => t.企业排口ID == port.ID)
                    .Where(t => t.QN.Equals(qn)).ToList();
                Log.Info("阀门控制记录条:" + items.Count);
                foreach (var item in items)
                {
                    if (per.Trim().Equals("100")|| per.Trim().Equals("ON"))
                    {
                        item.阀门状态 = "开";
                    }
                    else if (per.Trim().Equals("0")|| per.Trim().Equals("000")|| per.Trim().Equals("OFF"))
                    {
                        item.阀门状态 = "关";
                    }
                    else
                    {
                        item.阀门状态 = per + "%";
                    }
                    //item.阀门状态 = per;
                    // Log.Info("item.阀门状态:" + item.阀门状态);
                }
                pCtx.SaveChanges();
                var monitors = pCtx.主实时监控
                    .Where(t => t.企业基本信息ID == port.企业基本信息ID).Where(t => t.企业排口ID == port.ID);
                Log.Info("主实时监控条:" + items.Count);
                foreach (var monitor in monitors)
                {
                    if (per.Trim().Equals("100") || per.Trim().Equals("ON"))
                    {
                        monitor.阀门状态 = "开";
                    }
                    else if (per.Trim().Equals("0") || per.Trim().Equals("000") || per.Trim().Equals("OFF"))
                    {
                        monitor.阀门状态 = "关";
                    }
                    else
                    {
                        monitor.阀门状态 = per + "%";
                    }
                    //monitor.阀门状态 = per;
                    monitor.阀门状态时间 = datatime;

                }
                pCtx.SaveChanges();

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }


    }
}
